package com.dmgis.hndz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.dmgis.hndz.service.BaseProductService;
import com.dmgis.hndz.common.utils.DateUtil;
import com.dmgis.hndz.mapper.BaseProductMapper;
import com.dmgis.hndz.pojo.BaseProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

@Service
public  class BaseProductServiceImpl implements BaseProductService {

    @Autowired
    private BaseProductMapper baseProductMapper;

    @Override
    public List<String> getProductName(String productName, String productType) {
        return baseProductMapper.selectProductName(productName, productType);
    }

    @Override
    public List<String> getId(String productName) {
        return baseProductMapper.selectId(productName);
    }

    @Override
    public List<BaseProduct> getList(String time1, String time2, String productType) {
        return baseProductMapper.selectList5(time1,time2,productType);
    }

    @Override
    public List<BaseProduct> getList1(String time1, String time2, String productType) {
        return baseProductMapper.selectList1(time1, time2, productType);
    }

    @Override
    public List<BaseProduct> getList2(String productName, String productType) {
        return baseProductMapper.selectList2(productName, productType);
    }

    @Override
    public Integer updateProduct(BaseProduct baseProduct) {
        LambdaUpdateWrapper<BaseProduct> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BaseProduct::getProductname,baseProduct.getProductname())
                .eq(BaseProduct::getProducttype,baseProduct.getProducttype());
        int rows = baseProductMapper.update(baseProduct,updateWrapper);
        return rows;
    }

    @Override
    public Integer save(BaseProduct baseProduct) {
        return baseProductMapper.insert(baseProduct);
    }




    /**
     * 获取BaseProduct表中type字段的所有类型
     * @return
     */
    @Override
    public List<BaseProduct> getTypeOfBaseProduct() {
        List<BaseProduct> productList = baseProductMapper.selectList(new LambdaQueryWrapper<BaseProduct>()
                .select(BaseProduct::getProducttype));
        List<BaseProduct> distinctProducts = productList.stream()
                .distinct()
                .collect(Collectors.toList());
        return distinctProducts;
    }

    /**
     * 根据时间段和预报类型，查询BaseProduct表
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param productType 产品类型
     * @return
     */
    @Override
    public List<BaseProduct> getForecasttimePeriodAndTypeOfBaseProduct(String startTime, String endTime, String productType) {
        String time1 = (startTime == null || startTime.isEmpty()) ? null : startTime;
        String time2 = (endTime == null || endTime.isEmpty()) ? null : endTime;
        Timestamp timestamp1 = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(time1));
        Timestamp timestamp2 = DateUtil.localDateTimeToTimestamp(DateUtil.stringToLocalDateTime(time2));
        return baseProductMapper.selectList(new LambdaQueryWrapper<BaseProduct>()
        .eq(StringUtils.isNotBlank(productType), BaseProduct::getProducttype, productType)
        .between(BaseProduct::getForecasttime, timestamp1, timestamp2));
    }

    @Override
    public Integer deleteByProductName(String productName) {
        return baseProductMapper.delete(Wrappers.<BaseProduct>lambdaQuery()
                .eq(BaseProduct::getProductname, productName));
    }

}
